# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=freetype
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.10.2
pkgrel=2
pkgdesc="TrueType font rendering library (mingw-w64)"
arch=('any')
url="https://www.freetype.org/"
license=(GPL2+ custom:FreeType)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!libtool' 'staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/freetype/freetype-${pkgver}.tar.xz{,.sig}
        freetype-2.10.0-enable-valid.patch
        freetype-2.10.0-enable-spr.patch
        freetype-2.5.1-enable-sph.patch)
sha256sums=('1543d61025d2e6312e0a1c563652555f17378a204a61e99928c9fcef030a2d8b'
            'SKIP'
            '30e84993f899b66c31451992667f7ac6cc9e0b19dfe2f47ccb546d77bbc77ef5'
            '0571f18d97834df020e4cbdb1338b14f9160669f868237080951e01f5b86727c'
            '324012ad68388d22c63368314721d2360b48dd40daa808d0383699a281d931b0')
validpgpkeys=('58E0C111E39F5408C5D3EC76C1A60EACE707FDA5')

prepare() {
  cd "${srcdir}/freetype-${pkgver}"
  plain "freetype-2.10.0-enable-valid.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-valid.patch"
  plain "freetype-2.10.0-enable-spr.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-spr.patch"

  # Disabled for now due to resistance
  # Kept here for easier rebuilds via ABS
  # https://bugs.archlinux.org/task/35274
  #patch -Np1 -i "${srcdir}/freetype-2.5.1-enable-sph.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  ../freetype-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-static \
    --enable-shared \
    --with-zlib \
    --with-bzip2 \
    --with-png \
    --with-harfbuzz \
    --enable-freetype-config

  make
}

package () {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install

  # Replace the absolute path.
  sed -i "s|$(cygpath -m /)|/|g" ${pkgdir}${MINGW_PREFIX}/bin/freetype-config

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/GPLv2.TXT"   "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GPLv2.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/FTL.TXT"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/FTL.TXT"
}
